package de.fhtrier.mp.wollmaus.gui;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

import de.fhtrier.mp.wollmaus.constants.Constants;
import de.fhtrier.mp.wollmaus.gamestate.GameState;

public class Reset {
	private GameState gameState;
	
	private boolean enabled;
	private int elapsedTime;
	private String message_part1;
	private String message_part2;
	private UnicodeFont font;

	@SuppressWarnings("unchecked")
	public Reset(GameState gameState) {
		this.gameState = gameState;
		
		java.awt.Font awtFont = new java.awt.Font("Georgia", java.awt.Font.BOLD, 32);
		//font = new TrueTypeFont(awtFont, true);
		font = new UnicodeFont(awtFont); 
		font.getEffects().add(new ColorEffect(java.awt.Color.white));
		font.addNeheGlyphs();
		try {
			font.loadGlyphs();
		}
		catch (SlickException e) {
			e.printStackTrace();
		}
	}

	public void playerFailed(String mes1, String mes2) {
		enabled = true;
		elapsedTime = 0;
		message_part1 = mes1;
		message_part2 = mes2;
	}

	public void update(GameContainer gc, int delta) {
		if (enabled == true) {
			elapsedTime += delta;
			if (elapsedTime > Constants.RESET_TIME_COUNT / 2) {
				gameState.resetLevel();
			}
			if (elapsedTime > Constants.RESET_TIME_COUNT) {
				enabled = false;
			}
		}
	}

	public void render(GameContainer gc, Graphics g) {
		// Reset macht schwarzen Bildschirm
		if (isEnabled()) {
			int a;
			if (elapsedTime < Constants.RESET_TIME_COUNT / 2) {
				a = (int) (elapsedTime / (Constants.RESET_TIME_COUNT / 2f) * 255);
			}
			else {
				a = (int) (255 - ((elapsedTime - (Constants.RESET_TIME_COUNT / 2f)) / (Constants.RESET_TIME_COUNT / 2f)) * 255);
			}
			Color c = new Color(0, 0, 0, a);

			g.setColor(c);
			g.fillRect(0, 0, gc.getWidth(), gc.getHeight());

			font.drawString(gc.getWidth() / 2 - (font.getWidth(message_part1) / 2), gc.getHeight() / 2 - (font.getHeight(message_part1) / 2), message_part1, Color.white);
			font.drawString(gc.getWidth() / 2 - (font.getWidth(message_part2) / 2), gc.getHeight() / 2 + (font.getHeight(message_part1) / 2), message_part2, Color.white);
		}
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
}
